package com.example.travelapplication.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.room.Room;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.travelapplication.R;
import com.example.travelapplication.activity.DetailActivity;
import com.example.travelapplication.adapter.FavorAdapter;
import com.example.travelapplication.dao.FavorDao;
import com.example.travelapplication.database.TravelDatabase;
import com.example.travelapplication.entity.Favor;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LikeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LikeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public LikeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment LikeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static LikeFragment newInstance(String param1, String param2) {
        LikeFragment fragment = new LikeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<Favor> list = new ArrayList<>();
    private FavorAdapter adapter;
    private TextView tv_none;
    private TravelDatabase travelDatabase;
    private FavorDao favorDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_like, container, false);
        init(view);
        return view;
    }

    private void init(View view){
        travelDatabase = Room.databaseBuilder(getContext(), TravelDatabase.class, "travelDb")
                .allowMainThreadQueries()
                .build();
        favorDao = travelDatabase.favorDao();

        list = favorDao.getAllFavor();
        adapter = new FavorAdapter(list, getContext(), new FavorAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), DetailActivity.class);
                intent.putExtra("id", list.get(position).attId);
                getContext().startActivity(intent);
            }
        });
        recyclerView = view.findViewById(R.id.rv_fav);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        swipeRefreshLayout = view.findViewById(R.id.refresh_fav);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
                Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show();
            }
        });

        tv_none = view.findViewById(R.id.tv_none);
        if(list.isEmpty()){
            tv_none.setVisibility(TextView.VISIBLE);
        }else{
            tv_none.setVisibility(TextView.INVISIBLE);
        }
    }

    private void refresh(){
        list = favorDao.getAllFavor();
        adapter.setData(list);
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);

        if(list.isEmpty()){
            tv_none.setVisibility(TextView.VISIBLE);
        }else{
            tv_none.setVisibility(TextView.INVISIBLE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        travelDatabase.close(); //关闭数据库
    }
}